import wx from 'weixin-js-sdk';
import fly from './interceptor';

class Auth {
    constructor() {

    }
    async requestConfig() {
        let payload = {
            share_url: encodeURIComponent(location.href)
        };
        let res = await fly.post("/index/index/jssdk_ini", `data=${JSON.stringify(payload)}`);
        if (res.code != 1) {
            this.showcommonAlert({
                word: res.msg || '网络错误',
                isshare: false
            })
            return;
        }
        this.wxconfig(res.result, this.ready, this.authfail);
    }
    wxconfig(params, success, error) {
        wx.config({
            debug: false,
            appId: params.appId,
            timestamp: params.timestamp,
            nonceStr: params.noncestr,
            signature: params.sign,
            jsApiList: ["onMenuShareAppMessage", "onMenuShareTimeline"]
        });
        success = success.bind(this);
        wx.ready(success);
        wx.error(error);
    }
    ready() {
        // 监听微信分享事件
        wx.onMenuShareAppMessage({
            title: '今年您要的五福维达送你！',
            desc: '维达送福，韧品年味，收集五福赢好礼！',
            link: 'http://weida.fengdao168.com/index/index/index',
            imgUrl: 'http://cdn.fedau.com/project/weida/share_mini_img.png',
            success: function (res) {
                if (location.href.indexOf("my.html") > -1) {
                    location.replace("./index.html");
                }
            },
            cancle: function () {
                console.log("cancle");
            }
        });
        wx.onMenuShareTimeline({
            title: '今年您要的五福维达送你！',
            link: 'http://weida.fengdao168.com/index/index/index',
            imgUrl: 'http://cdn.fedau.com/project/weida/share_mini_img.png',
            success: function (res) {
                if (location.href.indexOf("my.html") > -1) {
                    location.replace("./index.html");
                }
            },
            cancle: function () {
                console.log("cancle");
            }
        });
    }
    authfail() {
        alert(err.errMsg || '初始化失败');
    }
}

const auth = new Auth();
module.exports = auth;